<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 * @version		$Id: Template.php 108 2011-04-11 17:33:46Z ninhgio@gmail.com $
 */

class Modules_Mail_Services_Template
{
	public static function validate($data)
	{
		$validator = new Gio_Core_Validator();
		foreach ($data as $index => $value) {
			if (gettype($value) == 'string') {
				$data[$index] = trim($value);
			}
		} 
		$data['messages']['name'] = $validator->setString($data['name'])
												->validate(array('notNull', 'isUsername')
																, array(
																	'notNull' => 'template_name_notnull',
																	'isUsername' => 'template_name_invalid'
																)
														);
		$data['messages']['title'] = $validator->setString($data['title'])
												->validate(array('notNull'), array('notNull' => 'template_title_notnull'));
												
		$data['messages']['subject'] = $validator->setString($data['subject'])
												->validate(array('notNull'), array('notNull' => 'template_subject_notnull'));
												
		$data['messages']['body'] = $validator->setString($data['body'])
												->validate(array('notNull'), array('notNull' => 'template_body_notnull'));

		$data['messages']['from_mail'] = $validator->setString($data['from_mail'])
												->validate(array('notNull', 'isEmail')
																, array(
																	'notNull' => 'template_from_mail_notnull',
																	'isEmail' => 'template_from_mail_invalid'
																)
														);
														
		$data['messages']['from_name'] = $validator->setString($data['from_name'])
												->validate(array('notNull'), array('notNull' => 'template_from_name_notnull'));
												
		$data['messages']['reply_to_mail'] = $validator->setString($data['reply_to_mail'])
												->validate(array('notNull', 'isEmail')
																, array(
																	'notNull' => 'template_reply_to_mail_notnull',
																	'isEmail' => 'template_reply_to_mail_invalid'
																)
														);
														
		$data['messages']['reply_to_name'] = $validator->setString($data['reply_to_name'])
												->validate(array('notNull'), array('notNull' => 'template_reply_to_name_notnull'));
												
		$error = false;
		foreach ($data['messages'] as $index => $message) {
			if ($message) {
				$data['messages_error'] = true;
				return $data;
			}
		}										
		$data['messages_error'] = false;
		return $data; 
	}
	
	public static function getById($id)
	{
		$conn = Gio_Db_Connection::getConnection();
		$dao = new Modules_Mail_Models_Mysql_Template();
		$dao->setConnection($conn);
		return $dao->getById($id);
	}
	
	public static function getTemplate($id, $userId)
	{
		$conn = Gio_Db_Connection::getConnection();
		$dao = new Modules_Mail_Models_Mysql_Template();
		$dao->setConnection($conn);
		return $dao->getTemplate($id, $userId);
	}
	
	/**
	 * Add new template
	 * 
	 * @param array
	 */
	public static function add($template)
	{
		$conn = Gio_Db_Connection::getConnection();
		$dao = new Modules_Mail_Models_Mysql_Template();
		$dao->setConnection($conn);
		return $dao->add($template);
	}
	
	public static function update($template)
	{
		$conn = Gio_Db_Connection::getConnection();
		$dao = new Modules_Mail_Models_Mysql_Template();
		$dao->setConnection($conn);
		return $dao->update($template);
	}

	public static function getByName($name)
	{
		$conn = Gio_Db_Connection::getConnection();
		$dao = new Modules_Mail_Models_Mysql_Template();
		$dao->setConnection($conn);
		return $dao->getByName($name);
	}
	
	public static function delete($id)
	{
		$conn 		 = Tomato_Db_Connection::factory()->getMasterConnection();
		$templateDao = Tomato_Model_Dao_Factory::getInstance()
											   ->setModule('mail')
											   ->getTemplateDao()
											   ->setDbConnection($conn);
		$template 	 = $templateDao->getById($id);
		$user     	 = Zend_Auth::getInstance()->getIdentity();
		
		if ($template == null || $template->locked == 1 
			|| $template->created_user_id != $user->user_id) 
		{
			return false;	
		} else {
			$templateDao->delete($id);
			return true;
		}
	}
	
	public static function getTemplates($user, $offset = null, $count = null)
	{
		$conn = Gio_Db_Connection::getConnection();
		$dao = new Modules_Mail_Models_Mysql_Template();
		$dao->setConnection($conn);
		return $dao->getTemplates($user['user_id'], $offset, $count);
	}
	
	public static function count($user)
	{
		$conn = Gio_Db_Connection::getConnection();
		$dao = new Modules_Mail_Models_Mysql_Template();
		$dao->setConnection($conn);
		return $dao->count($user['user_id']);
	}
}
